package com.zygh.policeasset.mapper;

import com.zygh.policeasset.pojo.ProjectBudgetReview;
import com.zygh.policeasset.pojo.ProjectDesign;
import com.zygh.policeasset.pojo.vo.ProjectBudgetReviewVO;
import com.zygh.policeasset.pojo.vo.ProjectDesignVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProjectDesignMapper {

    @Select({"<script>" +
            "select pd.*,p.name as projectName,af.app_status as appStatus," +
            "af.approval_steps_id as approvalStepsId,u.name as userName from project_design pd " +
            "left join project p on pd.project_no = p.project_no " +
            "left join audit_flow af on pd.flow_no = af.flow_no " +
            " left join user u on af.applicant = u.id  "+
            "<where>" +
            "<if test='projectNo != null and projectNo.length &gt; 0'> pd.project_no = #{projectNo} </if>" +
            "<if test='time1 != null and time2 != null and time1.length &gt; 0 and time2.length &gt; 0'> and pd.create_time between #{time1} and #{time2} </if>" +
            "<if test='userId != null '> and af.applicant=#{userId} </if>" +
            "<if test=' status != null'> and af.app_status = #{status} </if>" +
            "</where> " +
            " order by pd.id desc" +
            "</script>"
    })
    @Results(id = "map", value = {
            @Result(property = "projectNo", column = "project_no"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "sectionIds", column = "section_ids"),
            @Result(property = "financialEstimates", column = "financial_estimates"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "flowNo", column = "flow_no"),
            @Result(property = "updateTime", column = "update_time")})
    List<ProjectDesignVO> findAll(@Param("status") Integer status,
                                  @Param("projectNo") String projectNo,
                                  @Param("time1") String time1,
                                  @Param("time2") String time2,
                                  @Param("userId") Integer userId);

    @Insert({"insert into project_design (project_no,section_ids,rate," +
            "financial_estimates,amount,unit,flow_no,user_id,create_time) " +
            "values (#{projectNo},#{sectionIds},#{rate}," +
            "#{financialEstimates},#{amount},#{unit},#{flowNo}," +
            "#{userId},now())"})
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer add(ProjectDesign projectDesign);

    @Delete({"delete from project_design where id=#{id}"})
    Integer delete(Integer id);

    @Update({"<script>" +
            "update project_design " +
            "<set>" +
            "<if test='projectNo != null and projectNo.length &gt; 0'>" +
            "project_no = #{projectNo}, " +
            "</if>" +
            "<if test='financialEstimates != null '>" +
            "financial_estimates = #{financialEstimates}, " +
            "</if>" +
            "<if test='amount != null '>" +
            "amount = #{amount}, " +
            "</if>" +
            " update_time = now() " +
            "</set> where id = #{id}" +
            "</script>"})
    Integer update(ProjectDesign projectDesign);

    @Select({"select pd.*,p.name as projectName,af.app_status as appStatus," +
            " af.approval_steps_id as approvalStepsId,u.name as userName from project_design pd " +
            "left join project p on pd.project_no = p.project_no " +
            "left join audit_flow af on pd.flow_no = af.flow_no " +
            " left join user u on af.applicant = u.id  "+
            "where pd.id = #{id}"})
    @ResultMap("map")
    ProjectDesignVO findById(Integer id);
}